package controller;

import entity.MyResult;
import entity.StatisticsResult;
import entity.TimeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.StatisticsService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ray
 * @date 2020/6/20 10:40
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    private StatisticsService statisticsService;

    @ResponseBody
    @RequestMapping("/customerOrder")
    public MyResult<List<StatisticsResult>> list() {
        List<StatisticsResult> results = statisticsService.customerOrder();
        return new MyResult<>(0, "", results);
    }

    @ResponseBody
    @RequestMapping("/managerRank")
    public MyResult<List<StatisticsResult>> managerlist() {
        List<StatisticsResult> results = statisticsService.managerOrder();
        return new MyResult<>(0, "", results);
    }

    @ResponseBody
    @RequestMapping("StatisticsCustomers")
    public MyResult<Map<String, Object>> statisticsCustomers(@RequestParam HashMap<String, Object> params) {
        System.out.println(String.valueOf(params.get("date")));
        System.out.println(String.valueOf(params.get("action")));
        Map<String, Object> map = new HashMap<>();
        if (String.valueOf(params.get("action")).equals("year")) {
            TimeResult result = statisticsService.selectByYear(String.valueOf(params.get("date")));
            String[] date = {result.getTime()};
            int[] nums = {result.getNum()};
            map.put("time", date);
            map.put("nums", nums);
        } else if (String.valueOf(params.get("action")).equals("month")) {
            List<TimeResult> result = statisticsService.selectByMonth(String.valueOf(params.get("date")));
            List<String> list = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            for (TimeResult timeResult : result) {
                list.add(timeResult.getTime());
                list2.add(timeResult.getNum());
            }
            map.put("time", list);
            map.put("nums", list2);
        } else {
            List<TimeResult> result =
                    statisticsService.selectByDay(String.valueOf(params.get("date")), String.valueOf(params.get("days")));
            List<String> list = new ArrayList<>();
            List<Integer> list2 = new ArrayList<>();
            for (TimeResult timeResult : result) {
                list.add(timeResult.getTime());
                list2.add(timeResult.getNum());
            }
            map.put("time", list);
            map.put("nums", list2);
            System.out.println("====" + map);
        }
        return new MyResult<>(0, "", map);
    }

    /**
     * 分类客户统计
     */
    @ResponseBody
    @RequestMapping("/CustomerStatistics")
    public MyResult<List<StatisticsResult>> ClassifiedCustomerStatistics(@RequestParam HashMap<String, Object> params) {
        List<StatisticsResult> results = statisticsService.selectSort(String.valueOf(params.get("selectType")));
        for (StatisticsResult result : results) {
            System.out.println("==" + result);
        }
        return new MyResult<>(0, "", results);
    }
}
